using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.CustomPages;
using VSSDKAssist.Recipes.Common.Editors;
using VSSDKAssist.Recipes.Common.Converters;
using EnvDTE;
using VSSDKAssist.Assets.Helpers;
using VSSDKAssist.Enums;

namespace VSSDKAssist.Recipes.ConfigureVSPlk
{
	/// <summary>
	/// Custom page for ConfigureVSPlk recipe
	/// </summary>
	public partial class ConfigureVSPlkCP1 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigureVSPlkCP1"/> class.
		/// </summary>
		public ConfigureVSPlkCP1()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigureVSPlkCP1"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public ConfigureVSPlkCP1(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the name of the package.
		/// </summary>
		/// <value>The name of the package.</value>
		[RecipeArgument]
		public string PackageName
		{
			get { return (string)vePackageName.Value; }
			set
			{
				if(value != null)
				{
					vePackageName.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the package version.
		/// </summary>
		/// <value>The package version.</value>
		[RecipeArgument]
		public string PackageVersion
		{
			get { return (string)vePackageVersion.Value; }
			set
			{
				if(value != null)
				{
					vePackageVersion.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the VS edition.
		/// </summary>
		/// <value>The VS edition.</value>
		[RecipeArgument]
		public string VSEdition
		{
			get { return (string)veVSEdition.Value; }
			set
			{
				if(value != null)
				{
					veVSEdition.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the PKG company.
		/// </summary>
		/// <value>The name of the PKG company.</value>
		[RecipeArgument]
		public string PkgCompanyName
		{
			get { return (string)veCompanyName.Value; }
			set
			{
				if(value != null)
				{
					veCompanyName.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.vePackageName.BeginInit();
					this.vePackageName.ValueType = typeof(string);
					base.Site.Container.Add(vePackageName);
					this.vePackageName.EndInit();

					this.vePackageVersion.BeginInit();
					this.vePackageVersion.ValueType = typeof(string);
					base.Site.Container.Add(vePackageVersion);
					this.vePackageVersion.EndInit();

					this.veCompanyName.BeginInit();
					this.veCompanyName.ValueType = typeof(string);
					base.Site.Container.Add(veCompanyName);
					this.veCompanyName.EndInit();

					this.veVSEdition.BeginInit();
					this.veVSEdition.ValueType = typeof(string);
					base.Site.Container.Add(veVSEdition);
					this.veVSEdition.EditorInstance = new VSEditionEditor();
					this.veVSEdition.ConverterInstance = new VSEditionConverter();
					this.veVSEdition.EndInit();
				}
			}
		}

		private void vePackageName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string packageName = vePackageName.Value as string;
			DictionaryService.SetValue("PackageName", packageName);
		}

		private void veCompanyName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string companyName = veCompanyName.Value as string;
			DictionaryService.SetValue("PkgCompanyName", companyName);
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Double.Parse(System.String)"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		private void vePackageVersion_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			errorProvider.SetError(vePackageVersion, string.Empty);

			try
			{
				double.Parse(vePackageVersion.Value.ToString());
				DictionaryService.SetValue("PackageVersion", vePackageVersion.Value.ToString());
			}
			catch
			{
				DictionaryService.SetValue("PackageVersion", null);
				errorProvider.SetError(vePackageVersion, Properties.Resources.InvalidValue);
			}
		}

		private void veVSEdition_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string vsEdition = veVSEdition.Value as string;
			DictionaryService.SetValue("VSEdition", vsEdition);
		}
	}
}